package com.magoldeditor.view;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;

import com.magoldeditor.at.MaEditorXMLException;

/**
 * 
 * @author Administrator
 * @version v.2.0
 * 
 */

public class MaEditorButton extends JButton {
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 2L;
	
	/**
	 * default text
	 */
	private static final String TEXT_DEF = "button";
	
	/**
	 * 
	 */
	protected String name = "MaEditorButton";
	
	/**
	 * backGroundColor
	 */
	private Color BACKGROUND_COLOR = Color.black;
	
	/**
	 * default location
	 */
	private static final int X_DEF = 0;
	private static final int Y_DEF = 0;
	
	/**
	 * default size
	 */
	private static final int WIDTH_DEF = 100;
	private static final int HEIGHT_DEF = 20;
	
	/**
	 * 
	 */
	public MaEditorButton() {
		this(null);
	}
	
	/**
	 * 
	 * @param name
	 */
	public MaEditorButton(String name) {
		this(null, name);
	}
	
	/**
	 * 
	 * @param listener
	 * @param name
	 */
	public MaEditorButton(MouseListener listener, String name) {
		init(listener, name);
	}
	
	/**
	 * 
	 * @param listener
	 * @param name
	 */
	private void init(final MouseListener listener, final String name) {
		//contants
		ViewContants contants = ViewContants.getNewIntance();
		
		//name
		if (name != null) this.name = name;
		
		//text
		String text = contants.getComponentItem(name, ViewContants.TEXT_NAME);
		if (text != null) setText(text);
		else setText(TEXT_DEF);
		
		//config
		try {
			ViewContants.initConfig(this, this.name);
		} catch (MaEditorXMLException e) {
			e.printStackTrace();
			this.setBounds(X_DEF, Y_DEF, WIDTH_DEF, HEIGHT_DEF);
		}
		
		//visible
		this.setVisible(true);
		
		//color
		this.setBackground(BACKGROUND_COLOR);
		this.setForeground(BACKGROUND_COLOR);
		
		//refresh the ui
		this.repaint();
	}
	
	@Override
	public void finalize() {
		
	}
	
	@Override
	public String toString() {
		return this.getClass().getSimpleName() + " class ";
	}
	
	/**
	 * 
	 * @author Velconia
	 *
	 */
	class DefaultButtonListener implements MouseListener {
		@Override
		public void mouseClicked(MouseEvent e) {
			System.out.println(MaEditorButton.this.toString() + "Mouse Clicked");
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			System.out.println(MaEditorButton.this.toString() + "Mouse Entered");
		}

		@Override
		public void mouseExited(MouseEvent e) {
			System.out.println(MaEditorButton.this.toString() + "Mouse Exited");
		}

		@Override
		public void mousePressed(MouseEvent e) {
			System.out.println(MaEditorButton.this.toString() + "Mouse Pressed");
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			System.out.println(MaEditorButton.this.toString() + "Mouse Released");
		}
	}
}
